/*******************************************************************************
 * Copyright (c) 2011 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.wizard.panel.controller;

import it.txt.access.capability.factory.CapabilitySchemaFactoryException;
import it.txt.access.capability.factory.CapabilitySchemaFactory;
import it.txt.access.capability.factory.values.root.RootCapabilityValues;
import it.txt.access.capability.spi.ICapabilityPlugin;
import it.txt.access.capability.spi.resource.ICapabilityResourceID;
import it.txt.access.capability.wizard.Wizard;
import it.txt.access.capability.wizard.WizardPanelController;
import it.txt.access.capability.wizard.panel.PanelAccessTypePolicy;
import it.txt.access.capability.wizard.panel.exception.PanelAccessTypePolicyException;
import it.txt.access.capability.schema.AccessRightsCapabilityType;
import it.txt.access.capability.wizard.util.PluginsLoader;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class ControllerPanelAccessTypePolicy extends WizardPanelController {

    private PanelAccessTypePolicy panelAccessTypePolicy;

    public ControllerPanelAccessTypePolicy(Wizard wizard) {
        super(wizard);
        panelAccessTypePolicy = new PanelAccessTypePolicy(getWizard(), this);
        Date today = new Date();
        panelAccessTypePolicy.setMinSelectableDate(today);
    }

    @Override
    public Component getPanelComponent() {
        return panelAccessTypePolicy;
    }

    @Override
    public boolean aboutToDisplayPanel() {
        return true;
    }

    @Override
    public void displayingPanel() {
        getWizard().setBackButtonEnabled(true);
        getWizard().setNextButtonEnabled(true);
        getWizard().setFinishButtonEnabled(false);
        getWizard().setCancelButtonEnabled(true);
        getWizard().setAccessRightsCreationPercentage(50);
    }

    @Override
    public boolean aboutToHidePanel() {
        try {
            if (panelAccessTypePolicy.checkFormValidity()) {
                setForwardObject(createAccessRightsCapability());
                return true;
            }
        } catch (PanelAccessTypePolicyException ex) {
            JOptionPane.showMessageDialog(getWizard(),
                    ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return false;
    }

    public ArrayList<ICapabilityResourceID> getCapabilityResopurceIDTypes(){
        ArrayList<ICapabilityResourceID> iDs = new ArrayList<ICapabilityResourceID>();
        for(ICapabilityPlugin icp : PluginsLoader.PLUGIN_LIST){
            if(icp instanceof ICapabilityResourceID){
                iDs.add((ICapabilityResourceID) icp);
            }
        }
        return iDs;
    }

    private AccessRightsCapabilityType createAccessRightsCapability() throws PanelAccessTypePolicyException {
        RootCapabilityValues values = new RootCapabilityValues();
        values.setPolicyIssuer(panelAccessTypePolicy.getPolicyIssuerText());
        values.setPolicyAssignee(panelAccessTypePolicy.getAssigneeText());
        values.setPolicyResourceID(panelAccessTypePolicy.getPolicyResourceIDText());
        values.setRootCapabilityActions(panelAccessTypePolicy.getRootCapabilityActions());
        values.setPolicyRevopcationServices(panelAccessTypePolicy.getPolicyRevocationServices());
        values.setPolicyNotBeforeDate(panelAccessTypePolicy.getNotBeforeDate());
        values.setPolicyNotBeforeTime(panelAccessTypePolicy.getNotBeforeTime());
        values.setPolicyNotOrOnAfterDate(panelAccessTypePolicy.getNotOrOnAfterDate());
        values.setPolicyNotOrOnAfterTime(panelAccessTypePolicy.getNotOrOnAfterTime());
        try {
            return CapabilitySchemaFactory.getInstance().createAccessRights(values);
        } catch (CapabilitySchemaFactoryException ex) {
            throw new PanelAccessTypePolicyException(ex.getMessage(), ex);
        }
    }
}
